package com.example.demo.thread;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author Code Farmer
 * @date 2020/5/27 14:18
 */
public class SocketThread {

//    public static void main(String[] args) throws IOException {
//        ServerSocket serverSocket = new ServerSocket(9889);
//        while (true) {
//            Socket client = serverSocket.accept();
//
//            new Thread(() -> {
//                try {
//                    System.out.println("client port :" + client.getPort());
//                    InputStream inputStream = client.getInputStream();
//                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
//                    while (true) {
//                        System.out.println(bufferedReader.readLine());
//                    }
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }).start();
//        }
//
//    }

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9889);
        Socket client = serverSocket.accept();

        new Thread(() -> {
            for (; ; ) {
                try {
                    System.out.println("client port :" + client.getPort());
                    InputStream inputStream = client.getInputStream();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                    while (true) {
                        System.out.println(bufferedReader.readLine());
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();


    }

}
